# -*- coding: utf-8 -*-

import json
from public_util.util.AutoUrl import Route
from django.http import HttpResponse, Http404
from django.shortcuts import render_to_response
from package.models import Machine, MachineTask, Project


@Route()
def machine(request):
    """机器CRUD接口"""
    if request.method == "GET":
        req = request.REQUEST.get
        init = req("init", 0)
        if init:
            return render_to_response("machine/machine_index.html", locals())
        else:
            objs = Machine.objects.all()
            item_list = []
            for obj in objs:
                item_list.append([obj.id, obj.ip, obj.cpu_rate, obj.memory_rate,
                                  obj.hd_free, obj.remark, obj.get_state_display(), obj.id])
            return HttpResponse(json.dumps({"aaData": item_list}), content_type="application/json")
    elif request.method == "POST":
        err_msg = ""
        params = request.post_dic
        _id = request.GET.get("id", 0)
        if _id:
            try:
                Machine.objects.filter(id=_id).update(**params)
            except Exception as e:
                err_msg = e
        else:
            try:
                Machine(**params).save()
            except Exception as e:
                err_msg = e
        return render_to_response("feedback.html", locals())


@Route()
def machine_detail(request):
    """机器详情页面接口"""
    state_type = Machine.STATE_TYPE
    _id = request.REQUEST.get("id", 0)
    if _id:
        model = Machine.objects.filter(id=_id).first()
    return render_to_response("machine/machine_detail.html", locals())


@Route()
def machine_task(request):
    """机器任务CRUD接口"""
    if request.method == "GET":
        req = request.REQUEST.get
        init = req("init", 0)
        _id = req("id", 0)
        model = Machine.objects.filter(id=_id).first()
        if init:
            pro_objs = Project.filter_project(request)
            task_type = MachineTask.TASK_TYPE
            return render_to_response("machine/machine_task_index.html", locals())
        else:
            objs = MachineTask.filter_record(request)
            item_list = []
            for obj in objs:
                item_list.append([obj.project.pro_name, obj.get_task_type_display(), obj.id])
            return HttpResponse(json.dumps({"aaData": item_list}), content_type="application/json")
    elif request.method == "POST":
        err_msg = ""
        params = request.post_dic
        _id = request.GET.get("id", 0)
        params["project"] = Project.objects.filter(id=params.pop("pro_id")).first()
        params["machine"] = Machine.objects.filter(id=_id).first()
        try:
            MachineTask(**params).save()
        except Exception as e:
            err_msg = e
        return render_to_response("feedback.html", locals())
    elif request.method == "DELETE":
        err_msg = ""
        _id = request.REQUEST.get("id", 0)
        try:
            MachineTask.objects.filter(id=_id).delete()
        except Exception as e:
            err_msg = e
        return render_to_response("feedback.html", locals())


@Route()
def machine_task_detail(request):
    """机器任务详情页面接口"""
    task_type = MachineTask.TASK_TYPE
    project_objs = Project.filter_project(request)
    _id = request.REQUEST.get("id", 0)  # 机器任务详情页必须传machineID
    model = Machine.objects.filter(id=_id).first()
    return render_to_response("machine/machine_task_detail.html", locals())
